package cn.manyufun.myf.ui.main.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.manyufun.myf.R;
import cn.manyufun.myf.bean.NewsSummary;
import cn.manyufun.myf.bean.NewsSummaryList;
import cn.manyufun.myf.common.base.BaseFragment;
import cn.manyufun.myf.common.commonwidget.LoadingTip;
import cn.manyufun.myf.common.irecyclerview.IRecyclerView;
import cn.manyufun.myf.common.irecyclerview.OnLoadMoreListener;
import cn.manyufun.myf.common.irecyclerview.OnRefreshListener;
import cn.manyufun.myf.common.irecyclerview.widget.LoadMoreFooterView;
import cn.manyufun.myf.ui.main.activity.SearchResultActivity;
import cn.manyufun.myf.ui.news.adapter.NewsListAdapter;
import cn.manyufun.myf.ui.news.contract.NewsListContract;
import cn.manyufun.myf.ui.news.model.NewsListModel;
import cn.manyufun.myf.ui.news.presenter.NewsListPresenter;

/**
 * @author Kevin Xiao
 * @date Create Date: 2017-12-26
 * @Description
 */
public class NewsMainFragment extends BaseFragment<NewsListPresenter, NewsListModel> implements NewsListContract.View, OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.irc)
    IRecyclerView irc;
    @BindView(R.id.loadedTip)
    LoadingTip loadedTip;
    private NewsListAdapter newsListAdapter;
    private int mStartPage;
    private String mNewsId = "T1348648141035";
    private String mNewsType = "list";
    private List<NewsSummary> datas = new ArrayList<>();

    @OnClick(R.id.toolbar_ic_search)
    public void clickToSearch() {
        SearchResultActivity.startAction(getContext());
    }

    @Override
    public void showLoading(String title) {
        if (newsListAdapter.getPageBean().isRefresh()) {
            if (newsListAdapter.getSize() <= 0) {
                loadedTip.setLoadingTip(LoadingTip.LoadStatus.loading);
            }
        }
    }

    @Override
    public void stopLoading() {
        loadedTip.setLoadingTip(LoadingTip.LoadStatus.finish);
//        loadedTip.setLoadingTip(LoadingTip.LoadStatus.empty);
    }

    @Override
    public void showErrorTip(String msg) {
        if (newsListAdapter.getPageBean().isRefresh()) {
            if (newsListAdapter.getSize() <= 0) {
                loadedTip.setLoadingTip(LoadingTip.LoadStatus.error);
                loadedTip.setTips(msg);
            }
            irc.setRefreshing(false);
        } else {
            irc.setLoadMoreStatus(LoadMoreFooterView.Status.ERROR);
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.app_bar_news;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        irc.setLayoutManager(new LinearLayoutManager(getContext()));
        datas.clear();
        newsListAdapter = new NewsListAdapter(getContext(), datas);
//        newsListAdapter.openLoadAnimation(new ScaleInAnimation());

        irc.setAdapter(newsListAdapter);
        irc.setOnRefreshListener(this);
        irc.setOnLoadMoreListener(this);
        //数据为空才重新发起请求
        if (newsListAdapter.getSize() <= 0) {
            mStartPage = 1;
            mPresenter.getNewsListDataRequest(mNewsType, mNewsId, mStartPage);
        }
    }

    @Override
    public void onRefresh() {
        newsListAdapter.getPageBean().setRefresh(true);
        mStartPage = 1;
        //发起请求
        irc.setRefreshing(true);
        mPresenter.getNewsListDataRequest(mNewsType, mNewsId, mStartPage);
    }

    @Override
    public void onLoadMore(View loadMoreView) {
        newsListAdapter.getPageBean().setRefresh(false);
        //发起请求
        irc.setLoadMoreStatus(LoadMoreFooterView.Status.LOADING);
        mPresenter.getNewsListDataRequest(mNewsType, mNewsId, mStartPage);
    }

    @Override
    public void returnNewsListData(NewsSummaryList newsSummaryList) {
        List<NewsSummary> newsSummaries = newsSummaryList.getNewsList();
        if (newsSummaries != null && newsSummaries.size() > 0) {
            mStartPage += 20;
            if (newsListAdapter.getPageBean().isRefresh()) {
                irc.setRefreshing(false);
                newsListAdapter.replaceAll(newsSummaries);
                irc.setLoadMoreStatus(LoadMoreFooterView.Status.GONE);
            } else {
                if (newsSummaries.size() > 0) {
                    irc.setLoadMoreStatus(LoadMoreFooterView.Status.GONE);
                    newsListAdapter.addAll(newsSummaries);
                } else {
                    irc.setLoadMoreStatus(LoadMoreFooterView.Status.THE_END);
                }
            }
        } else {
            irc.setRefreshing(false);
            if (mStartPage == 1) {
                loadedTip.setLoadingTip(LoadingTip.LoadStatus.empty);
                irc.setLoadMoreStatus(LoadMoreFooterView.Status.GONE);
            } else {
                irc.setLoadMoreStatus(LoadMoreFooterView.Status.THE_END);
            }
        }
    }
}
